﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilverLightStreamingInterface;
using System.IO;

namespace t_SSI
{
    /// <summary>
    /// Summary description for t_FileUploadStateCache
    /// </summary>
    [TestClass]
    public class t_FileUploadStateCache
    {
        public t_FileUploadStateCache()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestCacheMarking()
        {
            ///
            /// See if the cache works
            /// 

            FileUploadStateCache_Accessor fc = new FileUploadStateCache_Accessor();

            var dir = fc.GetCacheForDirectory("c:\\junk\\dork");
            dir.Clear();
            dir.MarkFile("stuff.txt");
            Assert.IsTrue(dir.IsFileMarked("stuff.txt"), "Should be marked, but doesn't seem to be!");

            FileUploadStateCache_Accessor fc1 = new FileUploadStateCache_Accessor();
            var dir1 = fc.GetCacheForDirectory("c:\\junk\\dork");
            Assert.IsTrue(dir1.IsFileMarked("stuff.txt"), "Should be marked, but doesn't seem to be on new guys!");

            var dir2 = fc.GetCacheForDirectory("d:\\junk\\dork");
            Assert.IsFalse(dir2.IsFileMarked("stuff.txt"), "Stuff.txt on wrong disk is marked!");

            dir1.Clear();

            FileUploadStateCache_Accessor fc2 = new FileUploadStateCache_Accessor();
            var dir3 = fc2.GetCacheForDirectory("c:\\junk\\dork");
            Assert.IsFalse(dir3.IsFileMarked("stuff.txt"), "Should not be marked, but doesn't seem to be saved!");
            dir3.MarkFile("stuff.txt");

            ///
            /// Do a clear -- see how that goes...
            /// 

            fc2.ClearCacheStatus("c:\\junk");
            var fc3 = new FileUploadStateCache_Accessor();
            var dir4 = fc3.GetCacheForDirectory("c:\\junk\\dork");
            Assert.IsFalse(dir4.IsFileMarked("stuff.txt"), "Should not be marked! But it is!");

        }

        [TestMethod]
        public void TestCacheStamp()
        {
            ///
            /// Create a few files for ourselves
            /// 

            DirectoryInfo baseDir = new DirectoryInfo(Path.GetTempPath() + "\\TestCacheStamp");
            if (!baseDir.Exists)
            {
                baseDir.Create();
            }
            FileInfo myfile = new FileInfo(baseDir.FullName + "\\test.txt");
            using (TextWriter wr = myfile.CreateText())
            {
                wr.WriteLine("hi");
                wr.Close();
            }

            FileInfo mydummyfile = new FileInfo(baseDir.FullName + "\\bogus.txt");

            ///
            /// Now, test the stamping.
            /// 

            FileUploadStateCache_Accessor fc = new FileUploadStateCache_Accessor();
            var cdata = fc.GetCacheForDirectory(baseDir);
            cdata.Clear();

            cdata.MarkFile(myfile);
            Assert.IsTrue(cdata.IsFileMarked(myfile), "File is not marked, but it should be!");
            Assert.IsTrue(cdata.TimeOfMark(myfile) == myfile.LastWriteTime, "Time stamp for file is not right");

            Assert.IsFalse(cdata.IsFileMarked(mydummyfile), "Non-existant file is marked!?");
            Assert.IsTrue(cdata.TimeOfMark(mydummyfile) == new DateTime(), "Dummy file timestampe is not expected");
            Assert.IsFalse(cdata.IsFileMarked(mydummyfile), "Non-existant file is marked after doing the lookup?!?");
        }
    }
}
